<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" size="small"  :inline="true" v-show="showSearch" label-width="68px">
<!--            <el-form-item label="对象ID" prop="objId">-->
<!--                <el-input-->
<!--                        v-model="queryParams.objId"-->
<!--                        placeholder="请输入对象ID"-->
<!--                        clearable-->
<!--                        @keyup.enter.native="handleQuery"-->
<!--                />-->
<!--            </el-form-item>-->
            <el-form-item label="标题" prop="title">
                <el-input
                        v-model="queryParams.title"
                        placeholder="请输入标题"
                        clearable
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="状态" prop="state">
              <el-select
                v-model="queryParams.state"
                placeholder="请选择钱包到账状态"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.wallet_state"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="钱包途径" prop="classify">
              <el-select
                v-model="queryParams.classify"
                placeholder="请选择钱包来源途径"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.wallet_classify"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
<!--            <el-form-item label="关联ID" prop="relationId">-->
<!--                <el-input-->
<!--                        v-model="queryParams.relationId"-->
<!--                        placeholder="请输入关联ID"-->
<!--                        clearable-->
<!--                        @keyup.enter.native="handleQuery"-->
<!--                />-->
<!--            </el-form-item>-->
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
<!--            <el-col :span="1.5">-->
<!--                <el-button-->
<!--                        type="primary"-->
<!--                        plain-->
<!--                        icon="Plus"-->
<!--                        @click="handleAdd"-->
<!--                        v-hasPermi="['web:moneyItem:add']"-->
<!--                >新增</el-button>-->
<!--            </el-col>-->
<!--            <el-col :span="1.5">-->
<!--                <el-button-->
<!--                        type="success"-->
<!--                        plain-->
<!--                        icon="Edit"-->
<!--                        :disabled="single"-->
<!--                        @click="handleUpdate"-->
<!--                        v-hasPermi="['web:moneyItem:edit']"-->
<!--                >修改</el-button>-->
<!--            </el-col>-->
<!--            <el-col :span="1.5">-->
<!--                <el-button-->
<!--                        type="danger"-->
<!--                        plain-->
<!--                        icon="Delete"-->
<!--                        :disabled="multiple"-->
<!--                        @click="handleDelete"-->
<!--                        v-hasPermi="['web:moneyItem:remove']"-->
<!--                >删除</el-button>-->
<!--            </el-col>-->
            <el-col :span="1.5">
                <el-button
                        type="warning"
                        plain
                        icon="Download"
                        @click="handleExport"
                        v-hasPermi="['web:moneyItem:export']"
                >导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="moneyItemList" @selection-change="handleSelectionChange">
<!--            <el-table-column type="selection" width="55" align="center" />-->
<!--        <el-table-column label="明细ID" align="center" prop="itemId" v-if="true"/>-->
<!--        <el-table-column label="对象ID" align="center" prop="objId" />-->
            <el-table-column label="标题" align="center" prop="title" />
            <el-table-column label="工单号" align="center" prop="orderId" width="170">
              <template slot-scope="scope">
                <router-link :to="'/gongdan/data/index/' + scope.row.orderId" class="link-type">
                  <span style="color: #1890ff">{{scope.row.orderId}}</span>
                </router-link>
              </template>
            </el-table-column>
            <el-table-column label="收款方" align="center" prop="objName" width="120" />
            <el-table-column label="来源" align="center" prop="type">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.wallet_type" :value="scope.row.type"/>
              </template>
            </el-table-column>
            <el-table-column label="交易方式" align="center" prop="classify">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.wallet_classify" :value="scope.row.classify"/>
              </template>
            </el-table-column>
          <el-table-column label="金额(元)" align="center" prop="money">
            <template slot-scope="scope">
              <span>{{parseFloat(scope.row.money)}}</span>
            </template>
          </el-table-column>
            <el-table-column label="状态" align="center" prop="state">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.wallet_state" :value="scope.row.state"/>
              </template>
            </el-table-column>
          <el-table-column label="内容" align="center" prop="content" width="360"/>
          <el-table-column label="创建时间" align="center" sortable prop="createTime" width="180"/>
<!--                <template slot-scope="scope">-->
<!--                    <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>-->
<!--                </template>-->
<!--            </el-table-column>-->

<!--            <el-table-column label="关联ID" align="center" prop="relationId" />-->
<!--            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--                <template slot-scope="scope">-->
<!--                    <el-button-->
<!--                            type="text"-->
<!--                            icon="Edit"-->
<!--                            @click="handleUpdate(scope.row)"-->
<!--                            v-hasPermi="['web:moneyItem:edit']"-->
<!--                    >修改</el-button>-->
<!--                    <el-button-->
<!--                            type="text"-->
<!--                            icon="Delete"-->
<!--                            @click="handleDelete(scope.row)"-->
<!--                            v-hasPermi="['web:moneyItem:remove']"-->
<!--                    >删除</el-button>-->
<!--                </template>-->
<!--            </el-table-column>-->
        </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
        />

        <!-- 添加或修改钱包明细对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="moneyItemRef" :model="form" :rules="rules" label-width="80px">
                  <el-form-item label="对象ID" prop="objId">
                      <el-input v-model="form.objId" placeholder="请输入对象ID" />
                  </el-form-item>
                  <el-form-item label="标题" prop="title">
                      <el-input v-model="form.title" placeholder="请输入标题" />
                  </el-form-item>
                  <el-form-item label="内容">
                      <Editor v-model="form.content"/>
                  </el-form-item>
                  <el-form-item label="金额" prop="money">
                      <el-input v-model="form.money" placeholder="请输入金额" />
                  </el-form-item>
                  <el-form-item label="状态" prop="state">
                    <el-select v-model="form.state" placeholder="请选择状态">
                      <el-option
                        v-for="dict in dict.type.wallet_state"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                      ></el-option>
                    </el-select>
                  </el-form-item>
<!--                  <el-form-item label="状态" prop="state">-->
<!--                      <el-input v-model="form.state" placeholder="请输入状态" />-->
<!--                  </el-form-item>-->
                  <el-form-item label="关联ID" prop="relationId">
                      <el-input v-model="form.relationId" placeholder="请输入关联ID" />
                  </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script>
    import { listMoneyItem, getMoneyItem, delMoneyItem, addMoneyItem, updateMoneyItem } from "@/api/wallet/moneyItem";

    import Editor from "../../components/Editor";
    export default {
      name: "wallet",
      dicts: ['wallet_classify','wallet_state','wallet_type'],
      components: {Editor},
      data() {
        return {
          moneyItemList: [],
          open: false,
          buttonLoading: false,
          loading: true,
          showSearch: true,
          ids: [],
          single: true,
          multiple: true,
          total: 0,
          title: '',
          form: {},
          queryParams: {
            pageNum: 1,
            pageSize: 10,
            objId: undefined,
            title: undefined,
            type: undefined,
            classify: undefined,
            state: undefined,
            relationId: undefined,
            orderByColumn: 'createTime',
            isAsc: 'desc',
          },
          rules: {
            itemId: [
              { required: true, message: "明细ID不能为空", trigger: "blur" }
            ],
          }
        }
      },
      created() {
        this.getList();
      },
      methods: {
        /** 查询钱包明细列表 */
        getList() {
          this.loading = true;
          listMoneyItem(this.queryParams).then(response => {
            this.moneyItemList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        },
        // 取消按钮
        cancel() {
          this.open = false;
          this.reset();
        },
        // 表单重置
        reset() {
          this.form = {
            itemId: null,
            objId: null,
            title: null,
            content: null,
            type: null,
            classify: null,
            money: null,
            createTime: null,
            state: null,
            relationId: null
          };
          this.resetForm("moneyItemRef");
        },
        /** 搜索按钮操作 */
        handleQuery() {
          this.queryParams.pageNum = 1;
          this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
          this.resetForm("queryRef");
          this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
          this.ids = selection.map(item => item.itemId);
          this.single = selection.length != 1;
          this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
          this.reset();
          this.open = true;
          this.title = "添加钱包明细";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
          this.loading = true
          this.reset();
          const itemId = row.itemId || this.ids
          getMoneyItem(itemId).then(response => {
            this.loading = false;
            this.form = response.data;
            this.open = true;
            this.title = "修改钱包明细";
          });
        },
        /** 提交按钮 */
        submitForm() {
          this.$refs["moneyItemRef"].validate(valid => {
            if (valid) {
              this.buttonLoading = true;
              if (this.form.itemId != null) {
                updateMoneyItem(this.form).then(response => {
                  this.$modal.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }).finally(() => {
                  this.buttonLoading = false;
                });
              } else {
                addMoneyItem(this.form).then(response => {
                  this.$modal.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }).finally(() => {
                  this.buttonLoading = false;
                });
              }
            }
          });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
          const itemIds = row.itemId || this.ids;
          this.$modal.confirm('是否确认删除钱包明细编号为"' + itemIds + '"的数据项？').then(function() {
            this.loading = true;
            return delMoneyItem(itemIds);
          }).then(() => {
            this.loading = true;
            this.getList();
            this.$modal.msgSuccess("删除成功");
          }).finally(() => {
            this.loading = false;
          });
        },
        /** 导出按钮操作 */
        handleExport() {
          this.download('web/moneyItem/export', {
            ...this.queryParams
          }, `moneyItem_${new Date().getTime()}.xlsx`)
        }
      }
    }
</script>
